package com.qs.eggs.view.prodetail.fragment;

import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.base.fragment.BaseFragment;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class ProPicFragment extends BaseFragment {
    private RecyclerView recycleView;
    private String proid,type;
    private String note;
    private WebView webView;
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_details;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        proid=getActivity().getIntent().getStringExtra("proid");
        type=getActivity().getIntent().getStringExtra("type");
        webView=findView(R.id.webView);
        webView.getSettings().setJavaScriptEnabled(true);
        getPic();

    }
    //获得图文详情
    private void getPic(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("proid",proid);
        map.put("type",type);
        Gson gson=new Gson();
        String str=gson.toJson(map);

        OkGo.post(Constants.URL_BASE+"mall/showproduct?action=productDetail")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("图文详情--" + s);
                        try {
                            List<Map<String, Object>> piclist = new ArrayList<>();
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject jsonObject = jsonArray.getJSONObject(i);
                                note = jsonObject.getString("note");
                                webView.setWebViewClient(new MyWebViewClient());
                                webView.setWebChromeClient(new WebChromeClient());
                                webView.loadDataWithBaseURL("http://www.shouyaoguanjia.com/",note,"text/html","utf-8",null);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private class MyWebViewClient extends WebViewClient {

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            // html加载完成之后，添加监听图片的点击js函数
            //addImageClickListner();
            imgReset();
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
    /**
     * 循环遍历标签中的图片
     * js 语法
     */
    private void imgReset() {
        webView.loadUrl("javascript:(function(){" +
                "var objs = document.getElementsByTagName('img'); " +
                "for(var i=0;i<objs.length;i++)  " +
                "{"
                + "var img = objs[i];   " +
                "    img.style.maxWidth = '100%';   " +
                "}" +
                "})()");
    }
}
